<?php
/**
 * Created by PhpStorm.
 * User: 宋春宝
 * Date: 2018/11/7
 * Time: 10:40
 */

namespace app\wchat\controller;


use app\repository\PushMess;
use think\App;
use think\Request;

class Receipt extends Common
{
    protected $gid;
    public function __construct(App $app = null)
    {
        $row = db('admin')->where('admin_id',session('muid'))->find();
        $this->gid = $row['group_id'];
        parent::__construct($app);
    }

    public function auth(){
        return $this->fetch('auth');
    }

    public function index(){
        if($this->gid != 9) return $this->auth();
        return $this->fetch('list');
    }

    /**
     * 派单任务列表
     * @param Request $request
     * @return array
     */
    public function queryDis(Request $request){
        if($this->gid != 9) return $this->auth();
        $input = $request->param();
        $rows = db('dispatch')->alias('w')->order('w.id desc')->where(function ($query)use($input){

            $query->where('w.is_pub',1);
            $query->where('w.is_select',0);
            if(!empty($input['fault_id'])){
                $query->where('w.fault_id',$input['fault_id']);
            }
            if(!empty($input['state_id'])){
                $sis = explode(',',$input['state_id']);
                $query->where('w.state_id','in',$sis);
            }

            if(!empty($input['key'])){
                $query->where('w.title|ws.number|w.tel','LIKE','%'.$input['key'].'%');
            }
        })
            ->join('state st','st.id=w.state_id','left')
            ->join('fault ft','ft.id=w.fault_id','left')
            ->join('works ws','ws.id=w.works_id','left')
            ->join('admin a','a.admin_id=w.admin_id','left')
            ->field(['w.*','st.state_name','ft.title as fault_name','ws.attach as w_attach','ws.check_code','a.username'])
            ->paginate($input['limit'])
            ->toArray();
        return ['code'=>1,'msg'=>'获取成功!','data'=>$rows['data'],'count'=>$rows['total'],'rel'=>1];
    }

    /**
     * 详情页面
     * @return mixed
     */
    public function getDisUI(){
        if($this->gid != 9) return $this->auth();
        $id = input('id');

        if(empty($id)){
            $row = db('dispatch')->where('works_id',input('wid'))->field(['id'])->find();
            $id = $row['id'];
        }
        $info = db('dispatch')->alias('w')->where('w.id',$id)
            ->join('state st','st.id=w.state_id','left')
            ->join('fault ft','ft.id=w.fault_id','left')
            ->join('works ws','ws.id=w.works_id','left')
            ->join('admin a','a.admin_id=w.admin_id','left')
            ->join('admin reg','reg.admin_id=w.reg_id','left')
            ->field(['w.*','st.state_name','ft.title as fault_name','ws.attach as w_attach','ws.check_code','a.username','reg.username as reg_username','reg.name as reg_name'])
            ->find();
        $at = db('dispatch_at')->where(['dispatch_id'=>$id,'admin_id'=>session('muid')])->find();

        $info['is_conf'] = $at['is_conf'];
        $info['cause'] = $at['cause'];
        if(!$at) $info['none'] = 1;
        else $info['none'] = 0;

        return $this->fetch('getDis',['info'=>$info]);
    }

    /**
     *
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function receipt(){
        if($this->gid != 9) return $this->auth();
        $id = input('id',0);
        $info = db('dispatch')->where('id',$id)->find();
        if($info['is_pub']==1&&$info['is_select']==0){
            $row = db('dispatch')->where('id',$id)->update(['reg_id'=>session('muid'),'is_select'=>1,'state_id'=>21]);
            if($row){


                $info = db('admin')->where('admin_id',session('muid'))->find();
                $input = [];
                $input['icon']        = "/public/static/wx/img/progress-list/t1.png";
                $input['admin_id']    = session('muid');
                $input['c_time']      = date('Y-m-d H:i:s',time());
                $input['dispatch_id'] = $id;
                $input['content']     = $info['name']."已接单";
                $input['step']        = 1;
                db('dispatch_flow')->insert($input);
                $input['icon']        = "/public/static/wx/img/progress-list/t2.png";
                $input['admin_id']    = session('muid');
                $input['c_time']      = date('Y-m-d H:i:s',time());
                $input['dispatch_id'] = $id;
                $input['content']     = $info['name']."已组建团队";
                $input['step']        = 2;
                db('dispatch_flow')->insert($input);

                //接单人，发送消息提醒
                $uinfo = db('admin')->where('admin_id',session('muid'))->find();

                $data = [
                    'first'=>[
                        'value'=>"您好！{$info['number']}工单被{$uinfo['name']}接单！", 'color'=>'#000'
                    ],
                    "keyword1"=>["value"=>$info['number'], "color"=>"#173177"],
                    "keyword2"=>["value"=>date('Y-m-d H:i:s'), "color"=>"#173177"],
                    "remark"=>["value"=>"消息通知！", "color"=>"#173177"],
                ];
                PushMess::sendTemplate([$info['create_id']],$data,'NBeGDf2utQuF3NOMUJC1-uFZEINpeZu6PaasF_4_22A','');

                return ['code'=>1,'msg'=>'接单成功'];
            }

            return ['code'=>0,'msg'=>'接单失败'];
        }
        return ['code'=>0,'msg'=>'其他人已接单'];
    }
}